package com.jeasonzhao.commons.configuration;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import com.jeasonzhao.commons.utils.Algorithms;

public class ConfigurationAppender
{
    private String className = null;
    private String fileName = null;
    private String sectionName = null;
    private ConfigurationAppenderMode mode = null;
    public ConfigurationAppender()
    {
        super();
    }

    public void setClassName(String className)
    {
        this.className = className;
    }

    public void setFileName(String fileName)
    {
        this.fileName = fileName;
    }

    public void setSectionName(String sectionName)
    {
        this.sectionName = sectionName;
    }

    public void setMode(ConfigurationAppenderMode mode)
    {
        this.mode = mode;
    }

    public String getClassName()
    {
        return className;
    }

    public String getFileName()
    {
        return fileName;
    }

    public String getSectionName()
    {
        return sectionName;
    }

    public ConfigurationAppenderMode getMode()
    {
        return mode;
    }

    public IXMLConfiguration getConfigurationInstance()
        throws ConfigurationException
    {
        if(Algorithms.isEmpty(this.className))
        {
            return null;
        }
        try
        {
            @SuppressWarnings("unchecked")
            Class<IXMLConfiguration> cls = (Class<IXMLConfiguration>) Class.forName(this.className);

            this.className = cls.getName();
            if(IXMLConfiguration.class.isAssignableFrom(cls) == false)
            {
                throw new ConfigurationException("Can not convert class " + cls.getName() + " to " + IXMLConfiguration.class.getName());
            }
            Constructor<IXMLConfiguration> con = cls.getConstructor();
            con.setAccessible(true);
            return(IXMLConfiguration) con.newInstance();
        }
        catch(IllegalAccessException ex)
        {
            throw new ConfigurationException("Class Name " + this.className + "," + ex.getMessage(),ex);
        }
        catch(InstantiationException ex)
        {
            throw new ConfigurationException("Class Name " + this.className + "," + ex.getMessage(),ex);
        }
        catch(ClassNotFoundException ex)
        {
            throw new ConfigurationException("Class Name " + this.className + "," + ex.getMessage(),ex);
        }
        catch(SecurityException ex)
        {
            throw new ConfigurationException("Class Name " + this.className + "," + ex.getMessage(),ex);
        }
        catch(NoSuchMethodException ex)
        {
            throw new ConfigurationException("Class Name " + this.className + "," + ex.getMessage(),ex);
        }
        catch(InvocationTargetException ex)
        {
            throw new ConfigurationException("Class Name " + this.className + "," + ex.getMessage(),ex);
        }
        catch(IllegalArgumentException ex)
        {
            throw new ConfigurationException("Class Name " + this.className + "," + ex.getMessage(),ex);
        }

    }
}
